﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace _05_Monitor
{
    class Program
    {
       // private static int key = 5;

        void Thread1()
        {  
           
              Monitor.Enter(this);
           while (!Monitor.TryEnter(this))
            {
                Console.WriteLine("Thread {0} is waiting", Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(200);
            }
          

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("id: {0}; i = {1}", Thread.CurrentThread.ManagedThreadId, i);
                Thread.Sleep(100);
            }

           Monitor.Exit(this);

            //lock(this)
            //{
            //     for (int i = 0; i < 10; i++)
            //     {
            //         Console.WriteLine("id: {0}; i = {1}", Thread.CurrentThread.ManagedThreadId, i);
            //         Thread.Sleep(100);
            //     }
            //}
        }

        static void Main(string[] args)
        {
            Program pr = new Program();
            new Thread(pr.Thread1).Start();
            new Thread(pr.Thread1).Start();
            new Thread(pr.Thread1).Start();
        }
    }
}
